package com.buercorp.wangyu.core1.ch12.threads;

/**
 * 证明单个线程的 两条语句 未必是按顺序执行
 * 反正法 如果单线程 两条语句 是按顺序执行 应该出现 x = 0 y = 1 || x = 1 y = 0 || x = 1 y = 1 则不可能出现 x = 0 b = 0
 * @author liyiyu
 */
public class ThreadTxet11 {
    private static int x = 0, y = 0;
    private static int a = 0, b = 0;
    public static void main(String[] args) throws InterruptedException {
        int j = 0;
        for (;;){
            j++;
            x = 0; y = 0;
            a = 0; b = 0;
            Thread thread1 = new Thread(() ->{
                a = 1;
                x = b;
            });
            Thread thread2 = new Thread(() ->{
                b = 1;
                y = a;
            });
            thread1.start();
            thread2.start();
            thread1.join();
            thread2.join();
            if (x == 0 && y == 0) {
                System.out.println("第" + j + "次"+ "x = " + x + "y = " + y);
                break;
            }else {

            }
        }
    }
}
